package com.detrum.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.detrum.base.R;


/**
 * Created by abc on 2017/9/23.
 */

public class PermissionRegister {
    private final String TAG="PermissionRegister";
    private AppCompatActivity activity;
    public PermissionRegister(AppCompatActivity activity) {
        this.activity = activity;

    }

    public void CheckPermission(String permission,int msg,int flag, PermissionCall permissionCall){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(permission!=null&&permission.length()!=0){
                int i = activity.checkSelfPermission(permission);
                Log.i(TAG,"check i is "+i);
                if(i!= PackageManager.PERMISSION_GRANTED){
                    showDialogTipUserRequestPermission(new String[]{permission},msg,permissionCall);
                }else{
                    onPermissionGranted(permission,flag,permissionCall);
                }
            }
        }
    }

    private void showDialogTipUserRequestPermission(final String[] permission,int msg, final PermissionCall permissionCall) {
        Log.i(TAG,"showDialogTipUserRequestPermission\n"+permission[0]);
        if(permission.length==1) {
            if (permission[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                new AlertDialog.Builder(activity)
                        .setTitle(R.string.sdcard_title)
                        .setMessage(msg)
                        .setPositiveButton(R.string.open, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startRequestPermission(activity, permission);
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        permissionCall.Cancel();
                    }
                }).setCancelable(false)
                        .show();
            } else if (permission[0].equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
                Log.i(TAG,"AlertDialog is Create");
                new AlertDialog.Builder(activity)
                        .setTitle(R.string.location_title)
                        .setMessage(msg)
                        .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startRequestPermission(activity, permission);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        permissionCall.Cancel();
                    }
                }).setCancelable(false)
                        .create()
                        .show();
            } else if (permission[0].equals(Manifest.permission.ACCESS_COARSE_LOCATION)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    new AlertDialog.Builder(activity)
                            .setTitle(R.string.location_title)
                            .setMessage(msg)
                            .setPositiveButton(R.string.premission_open, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    startRequestPermission(activity, permission);
                                }
                            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            permissionCall.Cancel();
                        }
                    }).setCancelable(false)
                            .show();
                }
            }else if(permission[0].equals(Manifest.permission.CAMERA)){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    new AlertDialog.Builder(activity)
                            .setTitle(R.string.camera_title)
                            .setMessage(msg)
                            .setPositiveButton(R.string.premission_open, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    startRequestPermission(activity, permission);
                                }
                            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            permissionCall.Cancel();
                        }
                    }).setCancelable(false)
                            .show();
                }
            }
        }else if(permission.length>=2){
            new AlertDialog.Builder(activity)
                    .setTitle("权限不可用")
                    .setMessage("拍照需要相机和sd存储权限，蓝牙连接需要位置权限")
                    .setPositiveButton(R.string.premission_open, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startRequestPermission(activity, permission);
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    permissionCall.Cancel();
                }
            }).setCancelable(false)
                    .show();
        }
    }

    private static void startRequestPermission(Context context, String[] permission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ((Activity)context).requestPermissions(permission,100);
        }
    }

    private void onPermissionGranted(String permission,int flag, PermissionCall permissionCall) {
        switch (permission){
            case Manifest.permission.CAMERA:
                permissionCall.startPreview();
                break;
            case Manifest.permission.ACCESS_FINE_LOCATION:
                if(flag==4){
                    permissionCall.ScanWifi();
                }
                break;
            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
                if(flag==1){
                    permissionCall.getVersion(PermissionCall.VerFlag.MAIN);
                }else if(flag==2){
                    permissionCall.startUsbPreview();
                }else if(flag==5){
                    permissionCall.getVersion(PermissionCall.VerFlag.APP);
                }else if(flag==6){
                    permissionCall.getVersion(PermissionCall.VerFlag.IWT);
                }else if(flag==7){
                    permissionCall.getVersion(PermissionCall.VerFlag.DCAMH);
                }
                break;
            case Manifest.permission.ACCESS_COARSE_LOCATION:
                break;
        }
    }

}
